/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _MAIN_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "main.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
int main()
{
	int 	server_sockfd, client_sockfd;
	int 	server_len, client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	int 	result,x,y,z,nB,Nut;
	int	answer=0;
	int 	round = 0;
	char 	*buf;
	char 	number;

	/* Create an unnamed socket for the server. */
	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Assign a name to a socket. */
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = inet_addr("192.168.1.245");
	server_address.sin_port = 1803;
	server_len = sizeof(server_address);
	result = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
	if(result == -1)
	{
		perror("OOPS!!: bind function error\n");
		exit(1);
	}
		
	/* Create a connection queue and wait for clients. */
	result = listen(server_sockfd, 5);
	if(result == -1)
	{
		perror("OOPS!!: listen function error\n");
		exit(1);
	}

	while(1)
	{
		printf("server waiting round: %02d\n", round);
		round++;

		/* Accept a connection. */
		client_len = sizeof(client_address);
		client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);

		// Receive memory 
		result = read(client_sockfd, &nB, sizeof(int));
		if(result == -1)
		{
			write(client_sockfd,&answer,sizeof(int));
			printf("Read memory error\n");
		}
		else
		{
			// Answer to client
			answer = 1;
			write(client_sockfd,&answer,sizeof(int));
			answer = 0;
			// Receive start place
			result = read(client_sockfd,&number,sizeof(char) );
			if(result == -1)
			{
				write(client_sockfd,&answer,sizeof(int));
				printf("Read start number error\n");
			}
			else
			{
				// Answer to client
				answer = 1;
				write(client_sockfd,&answer,sizeof(int));
				answer = 0;
				// Receive string
				buf = (char*)malloc(nB*(sizeof(char)));
				result = read(client_sockfd,buf,nB*(sizeof(char)));
				if(result == -1)
				{
					printf("Read start number error\n");
				}
				else
				{
					// Answer to client
					answer = 1;
					write(client_sockfd,&answer,sizeof(int));
					answer = 0;

					if(number=='1')
					{			
						x = 1;
						y = 0;
						z = 0;
					}
					else if(number=='2')
					{
						x = 0;
						y = 1;
						z = 0;
					}
					else if(number=='3')
					{
						x = 0;
						y = 0;
						z = 1;
					}
					// find the Nut;
					Nut = find(&x,&y,&z,buf);
					// Send the position to client
					write(client_sockfd,&Nut,sizeof(int));
					free(buf);
				}
			}
		}

		/* Close socket. */
		close(client_sockfd);
	}

}

/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/
 
